layui.define(['admin','dltable', 'form'], function(exports){
    var $ = layui.$,admin = layui.admin,treeTable = layui.dltable,form = layui.form;

    admin.resize(function(){
        $('#tablePanel').height($(window).height() - 120);
    });

    treeTable.render({
        id:'LAY-list'
        ,elem: '#LAY-list'
        ,url: layui.setter.webRoot +'/agent/menu/list.data'
        ,method: 'post'
        ,parseData: function(res){
            return {
                "code": res.code,
                "msg": res.msg,
                "count": res.body.total,
                "data": res.body.data
            };
        }
        ,response: {statusCode: 200}
        ,treeId:'id'//树形id字段名称
        ,treeUpId:'parentId'//树形父id字段名称
        ,treeShowName:'name'//以树形式显示的字段
        ,cols: [[
            {title: '操作', width: 220, align:'center', toolbar: '#table-operate'}
            ,{field:'name', title: '菜单名称'}
            ,{field:'typeName', title: '菜单类型'}
            ,{field:'icon', title: '图标',templet: '#iconTpl',width:80}
            ,{field:'link',title: '链接'}
            ,{field:'permission',title: '权限'}
            ,{field:'priority', title: '优先级' }
        ]]
        ,page: false
        ,limit: 999
        ,height: 'full'
        ,done:function(data){
            admin.runResize();
        }
    });

    //监听搜索
    form.on('submit(LAY-list-search)', function(data){
        var field = data.field;
        //执行重载
        treeTable.reload('LAY-list', {
            where: field
            ,page: {curr:1}
        });
    });

    //事件
    var active = {
        add: function(){
            layer.open({
                type: 2
                ,title: '新增主菜单'
                ,content: layui.setter.webRoot +'/agent/menu/add.html'
                ,maxmin: false
                ,area: ['550px', '600px']
                ,btn: ['确定','取消']
                ,yes: function(index, layero){
                    var iframeWindow = window['layui-layer-iframe'+ index]
                        ,submit = layero.find('iframe').contents().find("#LAY-form-submit");
                    iframeWindow.layui.form.on('submit(LAY-form-submit)', function(data){
                        var field = data.field; //获取提交的字段
                        $.ajax({
                            type: "post",
                            url: layui.setter.webRoot +"/agent/menu/add.do",
                            data:field,
                            dataType:"json",
                            success:function(res){
                                if(res.code == 200){
                                    treeTable.reload('LAY-list'); //数据刷新
                                    layer.close(index); //关闭弹层
                                }else{
                                    layer.msg(res.msg,{icon: 2});
                                }
                            }
                        });
                    });
                    submit.click();
                }
            });
        }
        ,update:function(obj){
            layer.open({
                type: 2
                ,title: '修改菜单信息'
                ,content: layui.setter.webRoot +'/agent/menu/update.html?id='+obj.data.id
                ,maxmin: false
                ,area: ['550px', '600px']
                ,btn: ['确定','取消']
                ,yes: function(index, layero){
                    var iframeWindow = window['layui-layer-iframe'+ index]
                        ,submit = layero.find('iframe').contents().find("#LAY-form-submit");
                    iframeWindow.layui.form.on('submit(LAY-form-submit)', function(data){
                        var field = data.field; //获取提交的字段
                        $.ajax({
                            type: "post",
                            url: layui.setter.webRoot +"/agent/menu/update.do",
                            data:field,
                            dataType:"json",
                            success:function(res){
                                if(res.code == 200){
                                    treeTable.reload('LAY-list'); //数据刷新
                                    layer.close(index); //关闭弹层
                                }else{
                                    layer.msg(res.msg,{icon: 2});
                                }
                            }
                        });
                    });
                    submit.trigger('click');
                }
            });
        }
        ,addChild:function(obj){
            layer.open({
                type: 2
                ,title: '添加子菜单'
                ,content: layui.setter.webRoot +'/agent/menu/add.html?parentId='+obj.data.id
                ,maxmin: false
                ,area: ['550px', '600px']
                ,btn: ['确定','取消']
                ,yes: function(index, layero){
                    var iframeWindow = window['layui-layer-iframe'+ index]
                        ,submit = layero.find('iframe').contents().find("#LAY-form-submit");
                    iframeWindow.layui.form.on('submit(LAY-form-submit)', function(data){
                        var field = data.field; //获取提交的字段
                        $.ajax({
                            type: "post",
                            url: layui.setter.webRoot +"/agent/menu/add.do",
                            data:field,
                            dataType:"json",
                            success:function(res){
                                if(res.code == 200){
                                    treeTable.reload('LAY-list'); //数据刷新
                                    layer.close(index); //关闭弹层
                                }else{
                                    layer.msg(res.msg,{icon: 2});
                                }
                            }
                        });
                    });
                    submit.click();
                }
            });
        }
        ,del:function(obj){
            layer.confirm('确定要删除该菜单吗？', function(index){
                $.ajax({
                    type: "post",
                    url: layui.setter.webRoot +"/agent/menu/del.do",
                    data:{id:obj.data.id},
                    dataType:"json",
                    success:function(res){
                        if(res.code == 200){
                            obj.del();
                        }else{
                            layer.msg(res.msg,{icon: 5});
                        }
                    }
                });
                layer.close(index);
            });
        }
    };

    $('.layui-btn.LAY-btn-fun').on('click', function(){
        var type = $(this).data('type');
        active[type] ? active[type].call(this) : '';
    });

    treeTable.on('tool(LAY-list)', function(obj){
        var type = obj.event ;
        active[type] ? active[type].call(this,obj) : '';
    });

    exports('agent_menu_list', {})
});